/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/1/22 10:20
@File     : signup_request.go
@Project  : gohub
@Desc     :
*/
package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
	"gohub/app/requests/validators"
)

type SignUpPhoneExistRequest struct {
	Phone string `json:"phone,omitempty" valid:"phone"`
}

func ValidateSignupPhoneExist(data any, c *gin.Context) map[string][]string {
	//自定义验证规则
	rules := govalidator.MapData{
		"phone": []string{"required", "digits:11"},
	}

	//验证提示语
	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号必填，参数名 phone",
			"digits:手机号必须为11位数字",
		},
	}

	return validate(data, rules, messages)
}

type SignIUpEmailExistRequest struct {
	Email string `json:"email,omitempty" valid:"email"`
}

func ValidateSignUpEmailExist(data any, c *gin.Context) map[string][]string {
	//自定义验证规则
	rules := govalidator.MapData{
		"email": []string{"required", "min:4", "max:30", "email"},
	}
	//自定义消息提示
	messages := govalidator.MapData{
		"email": []string{
			"required:邮箱必填，参数名email",
			"min:太短了",
			"max:太长了",
			"email:邮箱格式有误",
		},
	}
	return validate(data, rules, messages)
}

// SignupUsingPhoneRequest 通过手机注册请求信息
type SignupUsingPhoneRequest struct {
	Phone           string `json:"phone,omitempty" valid:"phone"`
	VerifyCode      string `json:"verify_code,omitempty" valid:"verify_code"`
	Name            string `json:"name" valid:"name"`
	Password        string `json:"password,omitempty" valid:"password"`
	PasswordConfirm string `json:"password_confirm,omitempty" valid:"password_confirm"`
}

func SignupUsingPhone(data any, c *gin.Context) map[string][]string {
	//验证规则
	rules := govalidator.MapData{
		"phone":            []string{"required", "digits:11", "not_exists:users,phone"},
		"name":             []string{"required", "alpha_num", "between:3,20", "not_exists:users,name"},
		"password":         []string{"required", "min:6"},
		"password_confirm": []string{"required"},
		"verify_code":      []string{"required", "digits:6"},
	}
	//提示词
	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号必填",
			"digits:手机号必须为11位数字",
		},
		"name": []string{
			"required:用户名必填",
			"alpha_num:用户名格式错误，仅允许数字与英文",
			"between:用户名长度需在3-20之间",
		},
		"password": []string{
			"required:密码必填",
			"min:密码长度大于6位",
		},
		"password_confirm": []string{
			"rquired:确认密码必填",
		},
		"verify_code": []string{
			"required:验证码必填",
			"digits:验证码有误",
		},
	}

	errs := validate(data, rules, messages)

	_data := data.(*SignupUsingPhoneRequest)
	//验证两次密码是否一致
	errs = validators.ValidatePasswordConfirm(_data.Password, _data.PasswordConfirm, errs)
	//验证手机、邮箱验证码是否正确
	errs = validators.ValidateVerifyCode(_data.Phone, _data.VerifyCode, errs)
	return errs
}

// SignupUsingEmailRequest 通过邮箱注册请求信息
type SignupUsingEmailRequest struct {
	Email           string `json:"email,omitempty" valid:"email"`
	VerifyCode      string `json:"verify_code,omitempty" valid:"verify_code"`
	Name            string `json:"name" valid:"name"`
	Password        string `json:"password,omitempty" valid:"password"`
	PasswordConfirm string `json:"password_confirm,omitempty" valid:"password_confirm"`
}

func SignupUsingEmail(data any, c *gin.Context) map[string][]string {
	//验证规则
	rules := govalidator.MapData{
		"email":            []string{"required", "email", "not_exists:users,email"},
		"name":             []string{"required", "alpha_num", "between:3,20", "not_exists:users,name"},
		"password":         []string{"required", "min:6"},
		"password_confirm": []string{"required"},
		"verify_code":      []string{"required", "digits:6"},
	}
	//错误提示
	messages := govalidator.MapData{
		"Email": []string{
			"required:Email必填",
			"email:Email格式不正确",
			"not_exists:Email已被占用",
		},
		"name": []string{
			"required:用户名必填",
			"alpha_num:用户名格式错误，仅允许数字与英文",
			"between:用户名长度需在3-20之间",
		},
		"password": []string{
			"required:密码必填",
			"min:密码长度大于6位",
		},
		"password_confirm": []string{
			"rquired:确认密码必填",
		},
		"verify_code": []string{
			"required:验证码必填",
			"digits:验证码有误",
		},
	}

	errs := validate(data, rules, messages)

	_data := data.(*SignupUsingEmailRequest)
	//验证两次密码是否一致
	errs = validators.ValidatePasswordConfirm(_data.Password, _data.PasswordConfirm, errs)
	//验证手机、邮箱验证码是否正确
	errs = validators.ValidateVerifyCode(_data.Email, _data.VerifyCode, errs)
	return errs
}
